package com.csu.cphone.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.csu.cphone.dao.MaintainPhoneMapper;
import com.csu.cphone.pojo.MaintainPhonePojo;
import com.csu.cphone.pojo.MaintainProblemPojo;
import com.csu.cphone.service.MaintainPhoneService;
import com.csu.cphone.service.MaintainProblemService;

/**
 * @项目名称：cphone
 * @类名称：MaintainPhoneService
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年12月6日 下午9:13:26
 * @company:
 */
@Service
public class MaintainPhoneServiceImpl implements MaintainPhoneService
{
    @Autowired
    private MaintainPhoneMapper maintainPhoneMapper;

    @Autowired
    private MaintainProblemService maintainProblemService;

    /**
     * @see com.csu.cphone.service.MaintainPhoneService#add(java.lang.Integer,
     *      java.lang.String, java.lang.String)
     */
    @Override
    public MaintainPhonePojo add(Integer brandId, String name, String picUrl, List<MaintainProblemPojo> problems)
    {
        MaintainPhonePojo maintainPhone = new MaintainPhonePojo();
        maintainPhone.setBrandId(brandId);
        maintainPhone.setName(name);
        maintainPhone.setPicUrl(picUrl);
        maintainPhone.setStatus(0);
        maintainPhoneMapper.insert(maintainPhone);
        if (!CollectionUtils.isEmpty(problems))
        {
            for (MaintainProblemPojo problem : problems)
            {
                maintainProblemService.add(maintainPhone.getId(), problem.getProblemName(), problem.getProblemType(),
                        problem.getSelects());
            }
        }
        return maintainPhone;
    }

    /**
     * @see com.csu.cphone.service.MaintainPhoneService#update(com.csu.cphone.pojo.MaintainPhonePojo)
     */
    @Override
    public Integer update(MaintainPhonePojo maintainPhone)
    {
        return maintainPhoneMapper.update(maintainPhone);
    }

    /**
     * @see com.csu.cphone.service.MaintainPhoneService#queryById(java.lang.Integer)
     */
    @Override
    public MaintainPhonePojo queryById(Integer maintainPhoneId)
    {
        return maintainPhoneMapper.selectById(maintainPhoneId);
    }

    /**
     * @see com.csu.cphone.service.MaintainPhoneService#count()
     */
    @Override
    public Integer count()
    {
        return maintainPhoneMapper.count();
    }

    /**
     * @see com.csu.cphone.service.MaintainPhoneService#queryList(java.lang.Integer,
     *      java.lang.Integer, java.lang.String)
     */
    @Override
    public List<MaintainPhonePojo> queryList(Integer pageSize, Integer startIndex, String order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", pageSize);
        map.put("startIndex", startIndex);
        map.put("order", order);

        return maintainPhoneMapper.queryList(map);
    }

    /**
     * @see com.csu.cphone.service.MaintainPhoneService#deleteById(java.lang.Integer)
     */
    @Override
    public Integer deleteById(Integer maintainPhoneId)
    {
        return maintainPhoneMapper.deleteById(maintainPhoneId);
    }

}
